<template>
  <div class="edu-page-warp">
    <div class="">
      <el-card @reset="reset" @search="searchList">
        <div class="edu-page-title">社会实践课参数配置</div>
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-position="right" label-width="120px" class="formClass">
          <el-form-item label="课程代码：" prop="code" :rules="[
                  { required: true, message: $t('message.inputRequired'), trigger: 'blur' }
                ]">
            <el-col :span="8">
              <el-input v-model="ruleForm.code" placeholder="请输入"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item class="electiveclass" label="课程要求：必修" prop="electiveHour" :rules="[
                  { required: true, message: $t('请输入数字'), trigger: 'blur' }
                ]">
            <el-col :span="4">
              <el-input v-model="ruleForm.necessaryHour" type="number" min=0 placeholder="自动计算" onkeyup.native="value=value.replace(/[^\d.]/g,'')" disabled></el-input>
            </el-col>
            <el-col :span="3" style="text-align: center;">
              学时，选修
            </el-col>
            <el-col :span="4">
              <el-input v-model="ruleForm.electiveHour" type="number" min=0 placeholder="请输入" onkeyup.native="value=value.replace(/[^\d.]/g,'')"></el-input>
            </el-col>
            <span class="spnx">学时</span>
          </el-form-item>
          <el-button type="primary" @click="submitFormcode(1)" class="btnClass">保存</el-button>
        </el-form>
      </el-card>
      <el-card shadow="never">
        <el-table :order="false" :selection="true" :data="LaEducaDate">
          <el-table-column label="模块" prop="name" fixed="left" min-width="100">
            <template slot-scope="scope">
              <span :class="scope.row.nature=='1'?'red':''">{{scope.row.nature=='1'?'*':''}}{{scope.row.name}}</span>
            </template>
          </el-table-column>
          <el-table-column label="项目性质" prop="nature" min-width="100">
            <template slot-scope="scope">
              <span :class="scope.row.nature=='1'?'red':''">{{scope.row.nature=='1'?'必修':'选修'}}</span>
            </template>
          </el-table-column>
          <el-table-column label="认定学时上限" prop="necessaryHour" min-width="100" />
          <el-table-column :label="$t('message.operating')" fixed="right" width="130">
            <template slot-scope="scope">
              <el-button type="text" @click="handleClick('EDIT',scope.row)" size="mini">{{$t('message.edit')}}</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 弹框 -->
      <edu-dialog :title="dialogTitle" width="600px" ref="updateDialog" :auto-height="true" @close="resetAddForm">
        <el-form :model="createForm" ref="createForm" label-width="120px" label-suffix=":">
          <el-row>
            <el-col :span="12">
              <el-form-item label="板块" prop="name" :rules="[
                  { required: true, message: $t('message.inputRequired'), trigger: 'blur' }
                ]">
                <el-input v-model="createForm.name" placeholder="请输入" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目性质" prop="nature" :rules="[
                  { required: true, message: $t('message.inputRequired'), trigger: 'blur' }
                ]">
                <el-select v-model="createForm.nature" placeholder="请选择">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="有效学时要求" prop="necessaryHour" :rules="[
                  { required: true, message: $t('请输入数字'), trigger: 'blur' }
                ]">
                <el-input v-model="createForm.necessaryHour" type="number" min=0 placeholder="" onkeyup.native="value=value.replace(/[^\d.]/g,'')"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <span slot="buttons">
          <el-button @click="close(createForm)">{{$t('message.cancel')}}</el-button>
          <el-button @click="submitFormcode2(2)" type="primary" :disabled="btnTip">保存</el-button>
        </span>
      </edu-dialog>
    </div>
  </div>
</template>
<script>
import { queryDic } from 'common/src/api/dictionary'
import api from 'common/src/api/score/bachelor/labourCourse';
import UTILS from "common/src/utils/utils";
import dayjs from 'dayjs'
const defaultCreateForm = {
  name: '',
  type: '',
  nature: '',
  necessaryHour: '',
  electiveHour: ''
};

// 院聘导师库
export default {
  name: "",
  components: {

  },
  computed: {
  },
  mounted() {
    this.LaborEducationList();
  },
  data() {
    return {
      createForm: UTILS.clone(defaultCreateForm),
      LaEducaDate: [],
      codeList: [],
      dialogTitle: '',
      rows: {},
      options: [{
        value: '1',
        label: '必修'
      }, {
        value: '2',
        label: '选修'
      }],
      message: '',
      ruleForm: {
        name: '',
        type: '',
        nature: '',
        necessaryHour: '',
        electiveHour: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
        ],
        name2: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
        ],
      }
    };
  },
  filters: {
    filterNature: function (val) {
      if (val == 1) {
        return '必修'
      } else if (val == 2) {
        return '选修'
      } else if (val == 0) {
        return '选修'
      }
    }
  },
  watch:{
    'ruleForm.electiveHour'(newValue, oldValue) {
      let reg=/^\d+\.\d+$/;//是否是小数
      let reg2=/^\d{1,8}(\.\d{1,2})?$/;//是否是整数8位和小数2位
      if(!reg.test(newValue)){//不是小数
        this.ruleForm.electiveHour = newValue.substring(0, 8);
      }else{
        if(newValue.split('.')[1]&&newValue.split('.')[1].length>2){
          this.ruleForm.electiveHour = parseFloat(this.ruleForm.electiveHour).toFixed(2);
        }
      }
    },
    'createForm.necessaryHour'(newValue, oldValue) {
      let reg=/^\d+\.\d+$/;//是否是小数
      let reg2=/^\d{1,8}(\.\d{1,2})?$/;//是否是整数8位和小数2位
      if(!reg.test(newValue)){//不是小数
        this.createForm.necessaryHour = newValue.substring(0, 8);
      }else{
        if(newValue.split('.')[1]&&newValue.split('.')[1].length>2){
          this.createForm.necessaryHour = parseFloat(this.createForm.necessaryHour).toFixed(2);
        }
      }
    },
  },
  methods: {

    LaborEducationList() {
      api.LaborEducation().then(res => {
        if (res.code === 200) {
          this.LaEducaDate = res.data.filter(item => item.id != 1);//模块
          this.codeList = res.data.filter(item => item.id == 1);//课程代码
          if (this.codeList[0]) {
            this.ruleForm = this.codeList[0];
          }
        }
      });
    },

    //编辑
    handleClick(mode, row) {
      this.mode = mode;
      this.rows = row;
      let copy = Object.assign({}, row)
      this.createForm = copy;
      switch (mode) {
        case 'EDIT':
          this.dialogTitle = this.$t("message.edit");
          this.$refs.updateDialog.open();
          break
      }
    },

    //保存
    submitFormcode(Num) {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (this.ruleForm.necessaryHour < 0 || this.ruleForm.necessaryHour == '') {
            this.$message('必修不能是负数或空');
            return
          }
          if (this.ruleForm.electiveHour < 0 || this.ruleForm.electiveHour == '') {
            this.$message('选修不能是负数或空');
            return
          }
          let reg2=/^\d{1,8}(\.\d{1,2})?$/;//是否是整数8位和小数2位
          if(!reg2.test(parseFloat(this.ruleForm.electiveHour))){
            this.$message('选修学时不可大于8位数');
            return
          }
          let data = {
            id: 1,
            type: 1,
            code: this.ruleForm.code,
            necessaryHour: this.ruleForm.necessaryHour,
            electiveHour: this.ruleForm.electiveHour
          }
          api.LaborEducaSave(data).then(res => {
            if (res.code === 200) {
              this.LaborEducationList();
              this.$message({ type: 'success', message: res.msg })
            } else {
              this.$message({ type: 'error', message: res.msg })
            }
          });
        }
      });
    },
    //编辑保存
    submitFormcode2(Num) {
      this.$refs.createForm.validate(valid => {
        if (valid) {
          if (this.createForm.necessaryHour < 0) {
            this.$message('有效学时不能是负数');
            return
          }
          let reg2=/^\d{1,8}(\.\d{1,2})?$/;//是否是整数3位和小数2位
          if(!reg2.test(parseFloat(this.createForm.necessaryHour))){
            this.$message('有效学时要求不可大于8位数');
            return
          }
          let data = {
            id: this.createForm.id,
            type: this.createForm.type,
            name: this.createForm.name,
            nature: this.createForm.nature,
            necessaryHour: this.createForm.necessaryHour,
          }
          api.LaborEducaSave(data).then(res => {
            if (res.code === 200) {
              this.LaborEducationList()
              this.$refs.updateDialog.close();
              this.$message({ type: 'success', message: res.msg })
            } else {
              this.$message({ type: 'error', message: res.msg })
            }
          });
        }
      });
    },

    close(data) {
      //this.LaborEducationList();
      this.$refs.updateDialog.close();
    },
    resetAddForm() {
      //this.LaborEducationList();
      this.$refs.createForm.clearValidate();
    },

  }
};
</script>
<style lang="scss" scoped>
.edu-page-title {
    margin-bottom: 30px;
}
.line {
    text-align: center;
}
.spnx {
    display: inline-block;
    padding-left: 10px;
}
.el-select--small {
    width: 100%;
}
.formClass {
    position: relative;
}
.btnClass {
    position: absolute;
    bottom: 0;
    right: 0;
}
.red {
    color: red;
}
</style>
<style>
  .electiveclass .el-form-item__error{
    display: inline-block;
    position: relative;
    top: 27px;
    left: -176px;
}
</style>
